package com.lanou.pianke.admin.web.controller;

import com.lanou.pianke.admin.entity.Preply;
import com.lanou.pianke.admin.service.PcommentService;
import com.lanou.pianke.admin.service.PieceService;
import com.lanou.pianke.admin.service.PreplyService;
import com.lanou.pianke.piece.web.entity.Piece;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/admin/")
@RestController
public class PieceController {

    @Autowired
    private PieceService pieceService;

    @Autowired
    private PcommentService pcommentService;

    @Autowired
    private PreplyService preplyService;

    // 查看所有碎片
//    @GetMapping("showAllPieces")
//    public List<Piece> showAllPieces(){
//        List<Piece> pieceList = pieceService.showAll();
//        return pieceList;
//    }

    // 查看所有被举报数>10的碎片
    @GetMapping("showPieceReported")
    public List<Piece> showPieceReported(){
        return pieceService.showReported();
    }

    //删除所有被举报碎片
    @DeleteMapping("delete")
    public String delete(){
        pieceService.deleteReported();
        return "删除碎片";
    }

    // 删除某个被举报的碎片及碎片下的评论和回复
    @DeleteMapping("deleteByPieceid/{pieceid}")
    public void deleteByPieceid(@PathVariable String pieceid){
        pieceService.deleteByPieceid(pieceid);
        // 根据碎片id删除碎片评论
        pcommentService.deleteByPieceid(pieceid);
        // 根据碎片id查询碎片评论id
        String pcommentId = pcommentService.showPcommentIdByPieceid(pieceid);
        //  根据碎片评论id查询所有碎片回复
        List<Preply> preplyList = preplyService.showAllByPcommentId(pcommentId);
        if (preplyList != null){
            // 根据评论id删除该评论下的回复
            preplyService.deletePreply(pcommentId);
        }
    }

}
